﻿using System.Windows;
using System.Windows.Media.Animation;

namespace Heyo.Controls.Animations
{
    public class OpacityBorder : AnimationBorder
    {
        public static readonly DependencyProperty OpacityFromProperty = DependencyProperty.Register(
            "OpacityFrom", typeof(double), typeof(OpacityBorder), new PropertyMetadata(default(double)));

        public static readonly DependencyProperty OpacityToProperty = DependencyProperty.Register(
            "OpacityTo", typeof(double), typeof(OpacityBorder), new PropertyMetadata(1.0));

        public override Duration Duration { get; set; } = Duration.Automatic;
        public override double SpeedRatio { get; set; } = 1;

        public double OpacityFrom
        {
            get => (double) GetValue(OpacityFromProperty);
            set => SetValue(OpacityFromProperty, value);
        }

        public double OpacityTo
        {
            get => (double) GetValue(OpacityToProperty);
            set => SetValue(OpacityToProperty, value);
        }

        protected override AnimationTimeline InitAnimation()
        {
            DoubleAnimation animation = new DoubleAnimation(OpacityFrom, OpacityTo, Duration) { SpeedRatio = SpeedRatio };
            BeginAnimation(OpacityProperty, animation);

            return animation;
        }

        protected override AnimationTimeline InitInverseAnimation()
        {
            DoubleAnimation animation = new DoubleAnimation(OpacityTo, OpacityFrom, Duration) { SpeedRatio = SpeedRatio };
            BeginAnimation(OpacityProperty, animation);

            return animation;
        }

        protected override DependencyProperty AnimationProperty => UIElement.OpacityProperty;
        protected override IAnimatable AnimationElement => this;
    }
}